﻿#region using statements

using System;

using Microsoft.Xna.Framework;

using NGen;
using NGen.Managers.GUI;
using NGen.Managers.GameObject;

using Nuclex.UserInterface;
using Nuclex.UserInterface.Controls;
using Nuclex.UserInterface.Controls.Desktop;

using XNAGameDemo.GameObjects;
using XNAGameDemo.GameStates;

#endregion

namespace XNAGameDemo.GUI
{
	internal class GUIElementPlay1State : GUIElement
	{
		private readonly ButtonControl m_addButton = new ButtonControl();

		private readonly LabelControl m_label = new LabelControl();

		private readonly ButtonControl m_resetButton = new ButtonControl();

		private readonly ButtonControl m_scoreButton = new ButtonControl();

		private readonly LabelControl m_scoreLabel = new LabelControl();

		private readonly WindowWithOkButtonControl m_window = new WindowWithOkButtonControl();

		protected override void _Initialize() {
			m_window.Bounds = new UniRectangle(new UniScalar(0.5f, -250.0f), new UniScalar(0.25f, 0.0f), 500, 500);
			m_window.OkButton.Pressed += _OkButtonClicked;
			_Children.Add(m_window);

			m_label.Bounds = new UniRectangle(new UniScalar(0.075f, 0.0f), new UniScalar(0.2f, 0.0f), 0, 0);
			m_label.Text = "Score is displayed up top\n\n" + "To increase score, click on an object to destroy it\n\n" + "Add more objects by clicking the button at the top of screen";

			m_window.Children.Add(m_label);

			m_scoreLabel.Bounds = new UniRectangle(new UniScalar(0.47f, 0.0f), new UniScalar(0.2f, 0.0f), 0, 0);
			_Children.Add(m_scoreLabel);

			m_addButton.Bounds = new UniRectangle(new UniScalar(0.01f, 160.0f), new UniScalar(0.01f, 0.0f), new UniScalar(150), new UniScalar(30));
			m_addButton.Text = "Add 10 objects";
			_Children.Add(m_addButton);

			m_resetButton.Bounds = new UniRectangle(new UniScalar(0.01f, 320.0f), new UniScalar(0.01f, 0.0f), new UniScalar(150), new UniScalar(30));
			m_resetButton.Text = "Reset this game";
			_Children.Add(m_resetButton);

			m_scoreButton.Bounds = new UniRectangle(new UniScalar(0.01f, 480.0f), new UniScalar(0.01f, 0.0f), new UniScalar(300), new UniScalar(30));
			_Children.Add(m_scoreButton);
			m_scoreButton.Enabled = false;

			GameEngine.GetService<IManagerGameObjects>().SkipUpdate = true;
		}

		protected override void Update(GameTime p_gameTime) {
			m_scoreButton.Text = "Current score: " + GameEngine.GetService<GameStatePlay1.Play1Score>().m_points;
		}

		protected override void _Cleanup() {}

		protected void _OkButtonClicked(object p_sender, EventArgs p_e) {
			Console.WriteLine("ok");
			_Children.Remove(m_window);
			GameEngine.GetService<IManagerGameObjects>().SkipUpdate = false;
			m_resetButton.Pressed += _ResetButtonClicked;
			m_addButton.Pressed += _AddButtonClicked;
		}

		protected void _AddButtonClicked(object p_sender, EventArgs p_e) {
			Console.WriteLine("add");
			for(var i = 0; i < 10; ++i) {
				GameEngine.GetService<IManagerGameObjects>().AddObject(new BouncingBall());
			}
		}

		protected void _ResetButtonClicked(object p_sender, EventArgs p_e) {
			Console.WriteLine("reset");

			GameEngine.GetService<IManagerGameObjects>().DestroyAllGameObjects();
			for(var i = 0; i < 50; ++i) {
				GameEngine.GetService<IManagerGameObjects>().AddObject(new BouncingBall());
			}

			GameEngine.GetService<GameStatePlay1.Play1Score>().m_points = 0;
		}
	}
}